﻿using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using UnityEngine;

namespace ETModel
{
    /// <summary>
    /// DragCallback
    /// </summary>
    public class DragCallback
    {
        public delegate void DragDelegate(PointerEventData eventData);

        private List<DragDelegate> m_Listener = new List<DragDelegate>();

        public void Add(DragDelegate drag)
        {
            if (m_Listener.Contains(drag))
            {
                return;
            }

            m_Listener.Add(drag);
        }

        public void Remove(DragDelegate drag)
        {
            m_Listener.Remove(drag);
        }

        public void RemoveAll()
        {
            m_Listener.Clear();
        }

        public void Invoke(PointerEventData eventData)
        {
            for (int i = 0; i < m_Listener.Count; i++)
            {
                m_Listener[i].Invoke(eventData);
            }
        }
    }

    /// <summary>
    /// ButtonDragComponent
    /// </summary>
    public class ButtonDragComponent : Button, IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        public DragCallback onBeginDrag;
        public DragCallback onDrag;
        public DragCallback onEndDrag;
        public ScrollRect ScrollRect;
        public static bool StopScroll;

        override protected void Awake()
        {
            StopScroll = true;
            ScrollRect = transform.parent.parent.parent.GetComponent<ScrollRect>();
            onBeginDrag = new DragCallback();
            onDrag = new DragCallback();
            onEndDrag = new DragCallback();
        }

        /// <summary>
        /// OnBeginDrag
        /// </summary>
        /// <param name="eventData"></param>
        public void OnBeginDrag(PointerEventData eventData)
        {
            /* PointerEventData.position:*/
            if (ScrollRect != null)
            {
                ScrollRect.OnBeginDrag(eventData);
            }

            StopScroll = false;
            onBeginDrag?.Invoke(eventData);
        }

        /// <summary>
        /// OnDrag
        /// </summary>
        /// <param name="eventData"></param>
        public void OnDrag(PointerEventData eventData)
        {
            if (ScrollRect != null && !StopScroll)
            {
                ScrollRect.OnDrag(eventData);
            }
            else if (ScrollRect != null && StopScroll)
            {
                ScrollRect.OnEndDrag(eventData);
                onDrag?.Invoke(eventData);
            }
        }

        /// <summary>
        /// OnEndDrag
        /// </summary>
        /// <param name="eventData"></param>
        public void OnEndDrag(PointerEventData eventData)
        {
            if (ScrollRect != null && !StopScroll)
            {
                ScrollRect.OnEndDrag(eventData);
            }

            StopScroll = true;
            // StopScroll = false;
            onEndDrag?.Invoke(eventData);
        }
        
        /// <summary>
        /// 澶勭悊婊戝姩瑙﹀彂鐐瑰嚮
        /// </summary>
        /// <param name="eventData"></param>
        public override void OnPointerClick(PointerEventData eventData)
        {
            if (StopScroll)
            {
                base.OnPointerClick(eventData);
            }
        }
    }
}